<template>
  <div class="user">
    <el-form
      label-position="right"
      hide-required-asterisk
      :model="ruleForm"
    >
      <el-form-item label="手机号：">
        <el-input
          v-model="ruleForm.phone"
          placeholder="请输入注册手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机验证码：" class="code">
        <el-input
          v-model="ruleForm.code"
          placeholder="请输入验证码"
        ></el-input>
        <el-button
            style="width: 110px"
            class="codebtn btn"
            :disabled="isDisabled"
            type="primary"
            @click="sendCode"
            >{{ buttonText }}</el-button
          >
      </el-form-item>
      <el-form-item label="新密码：" prop="newPsd">
        <el-input
          v-model="ruleForm.newPassword"
          placeholder="请输入新密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码：">
        <el-input
          v-model="ruleForm.confirmPassword"
          placeholder="请确认新密码"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <div class="error-box">
      <div class="error" v-show="errmessage.err">
        <i class="el-icon-warning"></i>
        <span>{{ errmessage.message }}</span>
      </div>
    </div>
    <div>
      <el-button class="preservation" @click="submit()" type="primary"
        >保存</el-button
      >
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
export default {
  components: {},
  data() {
    return {
      buttonText: "获取验证码",
      isDisabled: false, // 是否禁止点击发送验证码按钮
      canSubmit: false,
      errmessage: {
        err: false,
        message: "手机号错误，请重输入",
      },
      ruleForm: {
        phone: "",
        code: "",
        newPassword: "",
        confirmPassword: "",
      },
    };
  },
  methods: {
    ...mapActions(["sendSms", "codeChangePwd"]),
     checkPhone() {
      if (!this.ruleForm.phone) {
        this.errmessage = {
          err: true,
          message: "请输入手机号",
        };
        this.canSubmit = false;
      } else if (!/^1[3|4|5|7|8|9][0-9]\d{8}$/.test(this.ruleForm.phone)) {
        this.errmessage = {
          err: true,
          message: "手机号错误，请重输入",
        };
        this.canSubmit = false;
      } else {
        this.errmessage = {
          err: false,
          message: "",
        };
        this.canSubmit = true;
      }
      return this.canSubmit;
    },
    checkPassword() {
      const pat = /^[0-9A-Za-z]{6,20}$/;
      if (!this.ruleForm.code) {
        this.errmessage = {
          err: true,
          message: "请输入验证码",
        };
        this.canSubmit = false;
      } else if (!this.ruleForm.newPassword) {
        this.errmessage = {
          err: true,
          message: "新密码不能为空",
        };
        this.canSubmit = false;
      } else if (!pat.test(this.ruleForm.newPassword)) {
        this.errmessage = {
          err: true,
          message: "新密码长度为6-20位，可以为数字、字母",
        };
        this.canSubmit = false;
      } else if (!this.ruleForm.confirmPassword) {
        this.errmessage = {
          err: true,
          message: "请确认密码",
        };
        this.canSubmit = false;
      } else if (
        this.ruleForm.confirmPassword != this.ruleForm.newPassword
      ) {
        this.errmessage = {
          err: true,
          message: "两次密码不一致",
        };
        this.canSubmit = false;
      } else {
        this.canSubmit = true;
      }
      return this.canSubmit;
    },
    // 发送验证码
    sendCode() {
     if (this.checkPhone()) {
        this.sendSms({ mobile: this.ruleForm.phone, type: 4 }).then((res) => {
          this.changeBtnText();
        });
      }
    },
    changeBtnText() {
      let time = 60;
      this.isDisabled = true;
      this.buttonText = "已发送（" + time + " s）";
      let timer = setInterval(() => {
        time--;
        this.buttonText = "已发送（" + time + " s）";
        if (time === 0) {
          clearInterval(timer);
          this.buttonText = "重新获取";
          this.isDisabled = false;
        }
      }, 1000);
    },
    submit() {
      if (this.checkPhone() && this.checkPassword()) {
        let data = {
          phone: this.ruleForm.phone,
          new_password: this.ruleForm.newPassword,
          phone_code: this.ruleForm.code,
        };
        this.codeChangePwd(data).then((res) => {
          this.$message({
            message: '修改成功',
            type: 'success'
          });
          this.$router.push({
            name:'wap-login'
          })
        });
      }
    },
  },
};
</script>

<style scoped>
.user {
  padding: 15px 5%;
  background: #fff;
  min-height: 50vh;
  box-sizing: border-box;
  width: 100%;
}
.user >>> .el-form-item__label {
  width: 90px;
  text-align: left;
  padding-right: 0;
}
.user >>> .el-form-item {
  display: flex;
  width: 100%;
  border-bottom: 1px solid #e6e6e6;
  margin: 15px 0;
  padding-bottom: 10px;
}
.user >>> .el-input__inner {
  border: none;
}
.preservation {
  width: 100%;
  height: 45px;
  background: #0e93e4;
  border-radius: 3px;
  font-size: 18px;
  font-family: Source Han Sans CN;
  font-weight: 500;
  color: #ffffff;
  margin-top: 30px;
  margin-bottom: 60px;
}
.code {
  position: relative;
}
.code >>> .el-form-item__content {
  flex: 1;
}
.code .el-button {
  width: 110px;
  padding: 0;
  height: 38px;
  position: absolute;
  right: 0;
  bottom: 0;
}
.error-box {
  height: 24px;
  margin-top: 20px;
}
.error {
  height: 22px;
  background: #feefef;
  border: 0.5px dashed #ffd5d5;
  font-size: 9px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #ff1e1e;
  line-height: 22px;
  margin-top: 10px;
}
.error i {
  font-size: 14px;
  margin: 0 7px;
}
</style>